package com.ccdt.tv.smartrecommend.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.PixelFormat;
import android.net.ConnectivityManager;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import com.blankj.utilcode.util.Utils;
import com.ccdt.tv.smartrecommend.R;
import com.ccdt.tv.smartrecommend.model.bean.ControlBean;
import com.ccdt.tv.smartrecommend.receiver.NetWorkStateReceiver;
import com.ccdt.tv.smartrecommend.view.widget.layout.FloatLayout;

/**
 * 控制推荐页面显示的 service
 * 通过windowManager实现在其他应用上显示智能推荐浮窗
 */
public class RecommendService extends Service {

    private final String TAG = "RecommendService";
    private FloatLayout mRootView;
    private WindowManager mWindowManager;
    private boolean isAdd = false;
    private NetWorkStateReceiver mNetWorkStateReceiver;
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(TAG, "onCreate 初始化智能推荐页面");
        initWindowAndView();
        //注册网络监听
        if (mNetWorkStateReceiver == null) {
            mNetWorkStateReceiver = new NetWorkStateReceiver();
        }
        IntentFilter networkfilter = new IntentFilter();
        networkfilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(mNetWorkStateReceiver, networkfilter);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.w(TAG, "onStartCommand: "+intent);
        String url = intent.getStringExtra("recommend_url");
        ControlBean bean = intent.getParcelableExtra("recommend_bean");
        Log.e(TAG, "onStartCommand url:" + url);
        if (!TextUtils.isEmpty(url)){
            mRootView.showWebViewByUrl(url);
        }else{
            mRootView.showRecommendView(bean);
        }

        if (isAdd) {
            mWindowManager.updateViewLayout(mRootView, getLayoutParams());
        } else {
            mWindowManager.addView(mRootView, getLayoutParams());
            isAdd = true;
        }

        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * 初始化窗口和View
     */
    private void initWindowAndView() {
        mWindowManager = (WindowManager) this.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        mRootView = new FloatLayout(mWindowManager);
        //监听释放资源
        mRootView.setFinishListener(mFinishListener);
        mRootView.setBackgroundResource(R.color.translucent_background);
        //屏蔽系统键盘
        InputMethodManager imm = (InputMethodManager) Utils.getApp().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(mRootView.getWindowToken(),0);
    }

    /**
     * 释放资源监听
     */
    private FloatLayout.FinishListener mFinishListener =
            new FloatLayout.FinishListener() {
                @Override
                public void finish() {
                    isAdd = false;
                    RecommendService.this.stopSelf();
                }
            };

    private WindowManager.LayoutParams getLayoutParams() {
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        layoutParams.format = PixelFormat.TRANSLUCENT;
        layoutParams.gravity = Gravity.CENTER | Gravity.TOP;
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
//        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        return layoutParams;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //解绑网络监听
        unregisterReceiver(mNetWorkStateReceiver);
    }
}
